package xyz.xtt.admin.controller;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.xtt.admin.request.DynamicsConfRequest;
import xyz.xtt.admin.service.IDynamicsConfService;
import xyz.xtt.common.response.ApiResult;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xugaopeng
 * @date 2024-12-13-16:41
 */
@RestController
@RequestMapping("/api/admin/dynamics_conf")
@Slf4j
public class DynamicsConfController {
    @Autowired
    IDynamicsConfService service;

    @GetMapping("/getExpandContent")
    public ApiResult<Map> getExpandContent(@Validated DynamicsConfRequest request) {
        String ma =service.getContent(request);
        Map<String, String> map = Arrays.stream(ma.split("&"))
                .map(s -> s.split("="))
                .collect(Collectors.toMap(arr -> arr[0], arr -> arr[1]));
        log.info("查询--key:{},内容为：{}",request.getKey(),ma);
        return ApiResult.ok(map);
    }


    @GetMapping("/getConf")
    public ApiResult<String> getConf(@Validated  DynamicsConfRequest request) {
        String str = StrUtil.toString(service.getContent(request));
        log.info("查询--key:{},内容为：{}",request.getKey(),str);
        return ApiResult.ok(str);
    }

//    @PostMapping("/add")
//    public ApiResult addInfo(@RequestBody TblDynamicsConf content) {
//        service.save(content);
//        return ApiResult.ok();
//    }

    @PostMapping("/update")
    public ApiResult updateInfo(@Validated @RequestBody DynamicsConfRequest content) {
        service.updateByKey(content);
        log.info("更新--key:{},内容为：{}",content.getKey(),content.getVal());
        return ApiResult.ok();
    }
}
